<?php
namespace Home\Controller;

class IdentifyController extends HomeController{
    
    /* 我的鉴定 */
    public function index(){
        !is_login() && $this->referer('User/login',U('Identify/index'));
        $type = I('request.type'); 
        $keyword=I('order_keyword');
        $M_checkup=M('checkup');
       
        
        empty($type) || in_array($type, array('pending', 'shipping', 'shipped', 'receipt', 'finish')) || $this->redirect('Identify/index');
        $p=I('p',1);
        $p <=0 && $p=1;
        $pagesize=8;
        $offset=($p-1)*$pagesize;
        $type=I('type');
        $map=array('uid'=>UID,'is_show'=>1);
        if($type){
            switch($type){
                case 'pending':
                    $map['status']=0;
                    break;
                case 'shipping':
                    $map['status']=1;
                    break;
                case 'shipped':
                    $map['status']=2;
                    break;
                case 'receipt':
                    $map['status']=array('between',array(3,4));
                    break;
                case 'finish':
                    $map['status']=5;
                    break;
            }
        }
        if($keyword){
           //$map['checkup_no|title']=array(intval($keyword),array('like','%'.$keyword.'%'),'_multi'=>true);
            $map['checkup_no|title']=array('like',array('like','%'.$keyword.'%'));
        }
        $total=$M_checkup->where($map)->count();
        $list=$M_checkup->where($map)->limit($offset,$pagesize)->order('id DESC')->select();
        int_to_string($list,array('status'=>getStatus('checkup_status')));
        foreach ($list as &$item){
            $item['image_path']=get_cover(get_table_field($item['id'],'checkup_id','cover_id','checkup_image'),'path');
        }
        /*统计信息*/
        $statistics=array();
        $statistics['pending']=$M_checkup->where(array('uid'=>UID,'status'=>0,'is_show'=>1))->count()|0;
        $statistics['shipping']=$M_checkup->where(array('uid'=>UID,'status'=>1,'is_show'=>1))->count()|0;
        $statistics['shipped']=$M_checkup->where(array('uid'=>UID,'status'=>2,'is_show'=>1))->count()|0;
        $statistics['receipt']=$M_checkup->where(array('uid'=>UID,'status'=>array('BETWEEN','3,4'),'is_show'=>1))->count()|0;
        $statistics['finish']=$M_checkup->where(array('uid'=>UID,'status'=>5,'is_show'=>1))->count()|0;
        $this->assign('statistics',$statistics);
        
        
        $this->assign('_list',$list);
        $__PAGE__ = new \Think\Page($total, $pagesize);
        $__PAGE__->lastSuffix = false;
        $__PAGE__->rollPage = 6;
        $__PAGE__->setConfig('prev', '<');
        $__PAGE__->setConfig('next', '>');
        $__PAGE__->setConfig('first', '<<');
        $__PAGE__->setConfig('last', '<span>...</span>');
        $__PAGE__->setConfig('header', '<span>共%TOTAL_PAGE%页</span>');
        $__PAGE__->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %END% %DOWN_PAGE% %HEADER%');
        
        $this->assign('_page', $__PAGE__->show());
        $this->assign('type', $type);
        $this->display();
    }
    
    /**
     * 详细
     */
    public function detail(){
        $checkupid=I('checkupid');
        $M_checkup=M('checkup');
        $M_checkup_shipping=M('checkup_shipping');
        
        $info=$M_checkup->find($checkupid);
        $info['image_path']=get_cover(get_table_field($info['id'],'checkup_id','cover_id','checkup_image'),'path');
        
       
        $M_payment_log=M('payment_log');
        if((int)$info['status']>=1){
            $tem_payment=$M_payment_log->where(array('checkup_id'=>$checkupid))->find();
            $order_info['pay_time']=$tem_payment['create_time'];
             
            $order_info['transaction_no']=$tem_payment['transaction_no'];
        }
        
        if((int)$info['status']>=2){
            $tem_order_shipping=$M_checkup_shipping->where(array('checkup_id'=>$checkupid,'type'=>'seller'))->find();
            
            $shipping_info=array();
            $shipping_info['shipping_code']=$tem_order_shipping['code'];
            $shipping_info['shipping_time']=$tem_order_shipping['create_time'];
            $shipping_info['shipping_company']=get_table_field($tem_order_shipping['shipping_code'],'code','title','shipping');
            $this->assign('shipping_info',$shipping_info);
        }
        
        $seller_shipping['shipping'] = getExpress($seller_shipping['shipping_code'], $seller_shipping['code']);
        $admin_shipping['shipping'] = getExpress($admin_shipping['shipping_code'], $admin_shipping['code']);
        $this->assign('seller_shipping', $seller_shipping);
        $this->assign('admin_shipping', $admin_shipping);
        
        $checkup_status=getStatus('checkup_status');
        $info['short_order']=$checkup_status[$info['status']];
        $this->assign('info',$info);
        
        $this->display();
    }
    /* 发布鉴定 */
    public function add(){

        if(IS_POST){
            $agreement=I('agreement');
            empty($agreement) && $this->error('请阅读并同意相关条款');
            
            $data=array();
            $data['title']=I('title');
            $data['category_id']=I('category_id');
            $data['quantity']=I('quantity');
            $data['province_id']=I('province_id');
            $data['city_id']=I('city_id');
            $data['area_id']=I('area_id');
            $data['address']=I('address');
            $data['mobile']=I('mobile');
            $data['realname']=I('realname');
            $data['description']=I('description');
            $image_cover_id=I('image_cover_id');            
            $movie_id=I('movie_id');
            
            !(preg_match('/.{4,60}$/', $data['title'])) && $this->error('标题2-50字这间');
            !get_table_field($data['category_id'], 'id', 'id', 'product_category') &&  $this->error('商品分类为空或没有找到匹配的分类！');
            !(preg_match('/[\d]+$/', $data['quantity']) && intval($data['quantity']) >= 0) &&  $this->error('请输入商品库存，不能小于0');
            !get_table_field($data['province_id'], 'province_id', 'province_id', 'hat_province') &&  $this->error('请选择发货地的省份');
            !get_table_field($data['city_id'], 'city_id', 'city_id', 'hat_city') &&  $this->error('请选择发货地的城市');
            !get_table_field($data['area_id'], 'area_id', 'area_id', 'hat_area') &&  $this->error('请选择发货地的区县');
            !preg_match('/^[\x{4e00}-\x{9fa5}][\x{4e00}-\x{9fa5}A-Za-z0-9_-]{1,50}+$/u', $data['address']) &&  $this->error('详细地址长度必须在2~50个字符');
            !preg_match('/1[34578]{1}\d{9}$/', $data['mobile']) &&  $this->error('联系电话为手机号码，请您正确填写');
            !preg_match('/^[\x{4e00}-\x{9fa5}]{1,4}+$/u', $data['realname']) &&  $this->error('请正确填写您的姓名');
            
            $data['uid']=UID;
            $data['create_time']=NOW_TIME;
            $data['status']=0;
            $data['identify_price'] = get_table_field($data['category_id'],'product_category_id','price','identify') | 0;
            
            $data['shipping_price']=get_table_field($data['province_id'],'province_id','price',
                'shipping_price')|0;
            $data['checkup_no']=build_no();
            $data['total']=(double)$data['identify_price'] * (int)$data['quantity'] + (double)$data['shipping_price'];
            $M_checkup = M('checkup');
            $M_checkup_image = M('checkup_image');
            $M_checkup_movie = M('checkup_movie');
            
            $id = $M_checkup->add($data);
            
            $id <= 0 && $this->error('发布鉴定失败');
            if($image_cover_id){
                $pics = $image_cover_id;
                !is_array($pics) && $pics = array($pics);
                foreach ($pics as $pic) {
                    $M_checkup_image->add(array('checkup_id'=>$id, 'cover_id'=>$pic));
                }
            }
           
            if($movie_id){
                $movies = $movie_id;
                !is_array($movies) && $movies = array($movies);
                foreach ($movies as $movie) {
                    $M_checkup_movie->add(array('checkup_id'=>$id, 'cover_id'=>$movie));
                }
            }
            $this->success('发布鉴定成功'); 
        }
        else{
            !is_login() && $this->referer('User/login',U('Identify/add'));
            
            $this->assign('category_tree', $this->getCategoryTree());
            $this->display();
        }
    }
    
    /* 支付鉴定费 */
    public function pay(){
        !is_login() && $this->referer('User/login',U('Identify/index'));
        $M_checkup=M('checkup');
        $checkupid=I('checkupid');
        $info=$M_checkup->find($checkupid);
        $info['full_address']=
        get_table_field($info['province_id'],'province_id','province','hat_province')
        .get_table_field($info['city_id'],'city_id','city','hat_city')
        .get_table_field($info['area_id'],'area_id','area','hat_area').$info['address'];
        $info['image_path']=get_cover(get_table_field($info['id'],'checkup_id','cover_id','checkup_image'),'path');
        $this->assign('info',$info);        
        $this->display();
    }
    
    /* 填写物流 */
    public function addShip(){
        !is_login() && $this->referer('User/login',U('Identify/index'));
        $checkupid=I('checkupid');
        $this->assign('checkupid',$checkupid);
        $ship_list=M('shipping')->select();
        $this->assign('shiping',$ship_list);
        $this->display();
    }
    
    /**
     * 获取商品分类树
     *
     * @return Ambigous <multitype:, mixed>
     */
    private function getCategoryTree(){
        static $list = array();
        if (empty($list)) {
            $list = S('CACHE_ADMIN_PRODUCT_CATEGORY');
        }
    
        if (empty($list)) {
            $M_product_category = M('product_category');
            $list = $M_product_category->order('sort ASC')->select();
            $list = list_to_tree($list);
    
            S('CACHE_ADMIN_PRODUCT_CATEGORY', $list);
        }
        return $list;
    }
}
?>